<?php

class Sistema_CorController extends App_Controller_Action
{
    private $_table     = 'COR';
    private $_pk        = 'COR_ID';
    private $_order_col = 'COR_ID';
    private $_order     = 'ASC';
    private $_sta_col   = 'COR_STA';
    private $obj        = '';
    private $form       = '';
    private $_file_col  = 'COR_FILE';
    public  $dir = 'public/upload/cor/';
    //public  $db = '';
     
    public function init()
    {   
        //$this->db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $this->obj = new Sistema_Model_DbTable_Cor();
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Sistema_Form_Cor();
    }

    public function indexAction() {
        Zend_Loader::loadClass('tools','library');
        
        $for_id = $this->_getParam('for_id', 0);
        $mat_id = $this->_getParam('mat_id', 0);
        $this->view->mat_id = $mat_id;
        $this->view->for_id = $for_id;
        
        if($mat_id == 0 || $for_id == 0){
            $urlOptions = array('controller'=>'material','action'=>'index');
            $this->_helper->redirector->gotoRoute($urlOptions);
        }
        
        $where = 'MAT_ID = '.$mat_id;
               
        //$STA = $this->_getParam($this->_sta_col, 1);
        //$where .= ' and ' . $this->_sta_col . ' = ' . $STA;
        
        $dscfiltro = $this->_getParam('dscfiltro', '');
        
        if($dscfiltro > '') $where .= ' and (COR_NOM like "%'. $dscfiltro.'%" or COR_REF = "'. $dscfiltro.'")';
        
        $this->view->dscfiltro = $dscfiltro;
        
        
        $result = $this->obj->fetchAll($this->obj->select()->where($where)->order($this->_order_col.' '.$this->_order));
        $this->view->result = $this->paginacao($result);
        
        $this->view->voltar =  $this->_urlbase . '/' . $this->_modulo . '/material/index/for_id/'.$for_id.'/mat_id/'.$mat_id;
    }
    
    public function editAction() {
       
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_geral.js');
        
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/colorpicker/css/colorpicker.css'); 
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/colorpicker/css/layout.css'); 
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/colorpicker/js/colorpicker.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/colorpicker/js/eye.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/colorpicker/js/utils.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/colorpicker/js/layout.js?ver=1.0.2');
        
        $for_id = $this->_getParam('for_id', 0);
        $mat_id = $this->_getParam('mat_id');
        if($mat_id == 0 || $for_id == 0){
            $urlOptions = array('controller'=>'material','action'=>'index');
            $this->_helper->redirector->gotoRoute($urlOptions);
        }
        
        $this->form->MAT_ID->setValue($mat_id);
        
        Zend_Loader::loadClass('imagem','library');
        
        $this->view->form = $this->form;
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if ($this->form->isValid($formData)) {
                    $id = (int) $this->form->getValue($this->_pk);
                    
                    $returnImg = imagem::upload($this->_file_col,$this->dir,$id);
                    
                    $data = $this->form->getValues();
                    unset($data['img2']);
                    unset($data[$this->_file_col]);
                    
                    if($id == 0){
                        $this->obj->insert($data);
                        $id =  $this->obj->getAdapter()->lastInsertId();    
                        imagem::rename($id,$this->dir,$returnImg['filenew']);
                    }
                    else $this->obj->update($data, $this->_pk.' = ' . $id);  
                    
                    if(strlen($_FILES[$this->_file_col]['tmp_name']) > 1){
                        $img = glob($this->dir.$id.'.*');
                        foreach($img as $filetemp) $filename = $filetemp;
                        imagem::reizeFull($filename,$filename, '30', '30');
                    }
                    
                    $urlOptions = array('controller'=>$this->_controller,'action'=>'index','for_id'=>$for_id,'mat_id'=>$mat_id);
                    $this->_helper->redirector->gotoRoute($urlOptions);
                    //$this->_helper->redirector('index');
                } else {
                    $this->form->populate($formData); 
                }
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.$ex->getMessage().'");</script>';
                echo $ex->getMessage();
                $this->form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $data = $this->obj->fetchRow($this->_pk . ' = ' . $id)->toArray();
                $this->form->populate($data);
            }
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        $for_id = $this->_getParam('for_id', 0);
        $mat_id = $this->_getParam('mat_id', 0);
        if ((int)$id > 0 ) {   
            //$data = array($this->_sta_col => 0);
            //$ok = (bool) $this->obj->update($data, $this->_pk . ' = ' . $id);  
            $ok = (bool) $this->obj->delete($this->_pk . ' = ' . $id); 
            
            $gradecor = new Sistema_Model_DbTable_GradecorHasCor();
            $gradecor->delete('COR_ID ='.$id);

            if($ok){
                foreach (glob($this->dir.$id.'.*') as $file) { 
                    unlink($file);
                }
            }
        }       
        //echo json_encode(array('ok' => $ok));
        $urlOptions = array('controller'=>$this->_controller,'action'=>'index','for_id'=>$for_id,'mat_id'=>$mat_id);
        $this->_helper->redirector->gotoRoute($urlOptions);
    }

}
